import { defineStore } from 'pinia';
import { useCartStore } from '@/store/cart';
import type { FansInfo } from '@/types/fans';

const cartStore = useCartStore();

export const useFansStore = defineStore({
  id: 'fans',
  state: () => ({
    fansinfo: <FansInfo>{
      avatar: '',
      nickname: '',
      username: '',
      phone: '',
      level_name: '',
      invite_code: '',
      created_at: '',
      token: '',
      refresh_token: '',
    },
    invite_qrcode: '',
    invite_poster: '',
  }),
  actions: {
    setFansInfo(info: FansInfo) {
      info.created_at = info.created_at.substring(0, 16);
      this.fansinfo = info;
    },

    // 清空用户信息
    clearFansInfo() {
      this.fansinfo = {
        avatar: '',
        nickname: '',
        username: '',
        phone: '',
        level_name: '',
        invite_code: '',
        created_at: '',
        token: '',
        refresh_token: '',
      };
      this.invite_qrcode = '';
      this.invite_poster = '';

      cartStore.cartNum = 0;
      cartStore.cartInit = 0;
      cartStore.cartReload = 0;
      cartStore.buySelect = [];
      cartStore.delSelect = [];
    },
  },
  unistorage: true, // 开启后采用localStorage对 state 的数据读写都将持久化
});
